//
//  PreservationV2TableViewCell.swift
//  AreaChainblock
//
//  Created by 赵佟越 on 2018/10/15.
//  Copyright © 2018年 zhaotongyue. All rights reserved.
//

import UIKit
import SwiftyUserDefaults
import SVProgressHUD

class PreservationV2TableViewCell: UITableViewCell
{
    var photoImageView : UIImageView?
    var titleLab:UILabel?
    var mybackView:UIView?
    var coverImageView:UIImageView?
    var desLab:UILabel?
    var mnType:String?
    var priceLab:UILabel?
    var numberButton = PPNumberButton.init(frame: CGRect(x:kScreenWidth/2 - 50 - 33 , y: 27.5, width: 140, height: 30))
    required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    
    
    }
    var titleLabel = UILabel.init(frame: CGRect(x: 70, y: 20, width: 60, height: 44))
    private var myModel:productModel? = productModel()
    private var num:NSString? = NSString()
    var numberM : NSString{
        set{
            num = newValue
            if self.mnType == "1" {
                titleLabel.text = String(format: "CD%@", num!)
            }else{
                titleLabel.text = String(format: "MT%@", num!)
            }
            
        }
        get{
            return (self.numberM)
        }
    }
    
    var model :productModel{
        set {
            myModel = newValue
            var num : Int = 0;
            if self.mnType == "1" {
                if myModel?.m_value == nil{
                    num = 0
                }else{
                    num = Int(Float((myModel?.m_value)!)!)
                }
                
            }else{
                if myModel?.n_value == nil{
                    num = 0
                }else{
                    num = Int(Float((myModel?.n_value)!)!)
                }
            }
            
            if self.mnType == "2"  {
                photoImageView?.image = UIImage.init(named: "组 6@2x")
            }else{
                photoImageView?.image = UIImage.init(named: "组 1@2x")
            }
            numberButton.maxValue = num
            numberButton.currentNumber =  String(format: "%d", num) 
        }
        get
        {
            return (self.myModel)!
            
        }
    }
    
    override var frame:CGRect{
    didSet {
    var newFrame = frame
    newFrame.origin.x += 20/2
    newFrame.size.width -= 20
    newFrame.origin.y += 0
    newFrame.size.height -= 0
    super.frame = newFrame
    }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    self.lauoutUI()
    
    }
    
    func lauoutUI()  {
        self.backgroundColor = UIColor.clear
        let backView = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth - 30, height: 85))
                backView.backgroundColor = UIColor.clear
                backView.layer.cornerRadius = 5
                self.addSubview(backView)
        
                photoImageView = UIImageView.init(frame: CGRect(x: 10, y: 20, width: 44, height: 44))
        backView.addSubview(photoImageView!)
        
        
        
        
        
                titleLabel.font = UIFont.boldSystemFont(ofSize: 16)
                titleLabel.adjustsFontSizeToFitWidth = true
               titleLabel.textColor = UIColor.white
        backView.addSubview(titleLabel)
        
                numberButton.borderColor(UIColor.gray)
                //开启抖动动画
               numberButton.backgroundColor = UIColor.clear
        numberButton.textField.textColor = UIColor.white
        numberButton.shakeAnimation = true
        
                numberButton.minValue = 0
                //加减按钮的闭包回调
                numberButton.numberResult { (number) in
        
                    print(number)
                }
        
                backView.addSubview(numberButton)
        
        
                let myButton1 = UIButton()
                backView.addSubview(myButton1)
                myButton1.snp.makeConstraints { (make) in
                    make.centerY.equalTo(backView)
                    make.right.equalTo(backView).offset(-10)
                    make.size.equalTo(CGSize(width: 80, height: 30))
                }
                myButton1.layer.cornerRadius = 15
                myButton1.backgroundColor = UIColor("#217AFF")
                myButton1.alpha = 0.7
                myButton1.titleLabel?.font = UIFont.systemFont(ofSize: 12)
                myButton1.setTitle(XALanguageManager.getString(key: "Preservation"), for: .normal)
        
                        myButton1.addTarget(self, action: #selector(PreservationV2TableViewCell.baozhi), for: .touchUpInside)
    }
    
    @objc func baozhi() {
        var type = "1"
        if self.mnType == "1" {
            type = "1"
        }else{
            type = "2"
        }
        
        MyNetwork.request(target: .baoZhiM(user_id: Defaults[.userID]!, trade_id: (myModel?.ids)!, value: numberButton.currentNumber!, type: type), success: { (result) in
                kLog(result)
                let needModel = BaseModel()
                needModel.yy_modelSet(with: result as! [AnyHashable : Any])
                
                if needModel.code == 1111{
                    SVProgressHUD.showSuccess(withStatus: XALanguageManager.getString(key: "Preservation Success"))
                    let theViewControllerYouSee = UIViewController.currentViewController() as! PrevationV2ViewController
                   
                    if self.mnType == "1"{
                        theViewControllerYouSee.getData()
                    }else{
                        theViewControllerYouSee.getNData()
                    }
                    
//                    self.navigationController?.pushViewController(ConfirmZhuCeViewController(), animated: true)
                    
                }else{
                    SVProgressHUD.showError(withStatus: XALanguageManager.getString(key: "失败"))
                }
                
            }) { (error) in
                //kLog(error.localizedDescription)
                SVProgressHUD.showError(withStatus: error.localizedDescription)
//                self.navigationController?.view.makeToast(error.localizedDescription)
            }
    }
    
    
    
    
    
    
    override func awakeFromNib() {
    
    super.awakeFromNib()
    
    // Initialization code
    
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
    
    super.setSelected(selected, animated: animated)
    
    
    
    // Configure the view for the selected state
    
    }
    
}
